function executeGrid(action_url,active)
{	
	if (typeof oTable == 'undefined') {
			 oTable = $('#example').dataTable( { 
				"sDom": '<"top"f>rt<"bottom"lpi><"clear">',
				//"sDom": 'rt<"bottom"lpi><"clear">',
				"sPaginationType": "full_numbers",
				"bJQueryUI": true,
				"bAutoWidth" : false,
				"aaSorting": [[0, 'DESC']],
				//"sScrollY": 280,
				//"sScrollX": "90%",
				//"sScrollXInner": "110%",
				"iDisplayLength": 10,
				"aLengthMenu": [[10,25, 50, 100], [10,25, 50, 100]],
				"bProcessing": true,
				"bServerSide": true,
				"bDestroy": true,
				//"bRetrieve": true,
				"sAjaxSource": action_url,
				"fnServerData": function ( sSource, aoData, fnCallback ) {
					/* Add some extra data to the aoData - FOR POSTING CUSTOM VALUES */
					//aoData.push( { "name": "active", "value": active } );
					//aoData.push( { "name": "sEcho", "value": $('#sEcho').val() } );
					$('#sEcho').val(aoData[0].value);
					$.ajax({
						"dataType": 'json', 
						"type": "POST", 
						"url": sSource, 
						"data": aoData,
						"success": fnCallback
					});
				}
			} );
			
			//document.getElementById('example_filter').style.display='none';
			
			
		}
		else
		{
			oTable.fnDraw();
			oTable.fnReloadAjax();
			oTable.fnReloadAjax(action_url);
			oTable.fnClearTable( 0 );
			oTable.fnDraw();
		}
		
		
	/*var buttonfunction = $('#ActivateBut').attr('onClick');
	alert(typeof(buttonfunction));
	alert(buttonfunction);
	var splitedFunction = buttonfunction.split(",");
	alert(splitedFunction[5]);
	splitedFunction[5]="'"+active+"');";
	var newFunction = splitedFunction.join(',');
	$('#ActivateBut').attr('onClick',newFunction);

	var buttonfunction = $('#DeactivateBut').attr('onClick');
	var splitedFunction = buttonfunction.split(",");
	splitedFunction[5]="'"+active+"');";
	newFunction = splitedFunction.join(',');
	$('#DeactivateBut').attr('onClick',newFunction);*/
	
}

function executefilter(action_url,val_array)
{
			 oTable = $('#example').dataTable( {
				"sDom": '<"top"f>rt<"bottom"lpi><"clear">',
				"sPaginationType": "full_numbers",
				"bJQueryUI": true,
				"bAutoWidth" : false,
				"aaSorting": [[0, 'DESC']],
				"iDisplayLength": 25,
				"aLengthMenu": [[25, 50, 100], [25, 50, 100]],
				"bProcessing": true,
				"bServerSide": true,
				"bDestroy": true,
				"sAjaxSource": action_url,
				"fnServerData": function ( sSource, aoData, fnCallback ) {
					/* Add some extra data to the aoData - FOR POSTING CUSTOM VALUES */
					for (i in val_array)
					{
						aoData.push( { "name": i, "value": val_array[i] } );
					}
					//aoData.push(  json_str  );
					$.ajax( {
						"dataType": 'json', 
						"type": "POST", 
						"url": sSource, 
						"data": aoData, 
						"success": fnCallback
					} );
				}
			} );
			
			//document.getElementById('example_filter').style.display='none';
	
}


function executeReportGrid(action_url)
{
			 oTable = $('#example').dataTable( {
				"sDom": '<"top"f>rt<"bottom"lpi><"clear">',
				"sPaginationType": "full_numbers",
				"bJQueryUI": true,
				"bAutoWidth" : false,
				"aaSorting": [[0, 'DESC']],
				"iDisplayLength": 25,
				"aLengthMenu": [[25, 50, 100], [25, 50, 100]],
				"aoColumns": [ null,null,{ "bSortable": false },{ "bSortable": false },{ "bSortable": false },{ "bSortable": false },{ "bSortable": false },null,null],
				"bProcessing": true,
				"bServerSide": true,
				"bDestroy": true,
				"sAjaxSource": action_url,
				"fnServerData": function ( sSource, aoData, fnCallback ) {
					$.ajax( {
						"dataType": 'json', 
						"type": "POST", 
						"url": sSource, 
						"data": aoData, 
						"success": fnCallback
					} );
				}
			} );
			
			//document.getElementById('example_filter').style.display='none';
			
			
		/*}
		else
		{


			oTable.fnDraw();
			//alert(typeof oTable);
			//oTable.fnReloadAjax();
			//oTable.fnReloadAjax(action_url);
			//oTable.fnClearTable( 0 );
			//oTable.fnDraw();
		}*/
		
		
	/*var buttonfunction = $('#ActivateBut').attr('onClick');
	alert(typeof(buttonfunction));
	alert(buttonfunction);
	/*var splitedFunction = buttonfunction.split(",");
	alert(splitedFunction[5]);
	splitedFunction[5]="'"+active+"');";
	var newFunction = splitedFunction.join(',');
	$('#ActivateBut').attr('onClick',newFunction);

	var buttonfunction = $('#DeactivateBut').attr('onClick');
	var splitedFunction = buttonfunction.split(",");
	splitedFunction[5]="'"+active+"');";
	newFunction = splitedFunction.join(',');
	$('#DeactivateBut').attr('onClick',newFunction);*/
	
}
function executeConnectionReportGrid(action_url)
{
			 oTable = $('#example').dataTable( {
				"sDom": '<"top"f>rt<"bottom"lpi><"clear">',
				"sPaginationType": "full_numbers",
				"bJQueryUI": true,
				"bAutoWidth" : false,
				"aaSorting": [[0, 'DESC']],
				"iDisplayLength": 25,
				"aLengthMenu": [[25, 50, 100], [25, 50, 100]],
				"bProcessing": true,
				"bServerSide": true,
				"bDestroy": true,
				"sAjaxSource": action_url,
				"fnServerData": function ( sSource, aoData, fnCallback ) {
					$.ajax( {
						"dataType": 'json', 
						"type": "POST", 
						"url": sSource, 
						"data": aoData, 
						"success": fnCallback
					} );
				}
			} );
			
			//document.getElementById('example_filter').style.display='none';
			
			
		/*}
		else
		{


			oTable.fnDraw();
			//alert(typeof oTable);
			//oTable.fnReloadAjax();
			//oTable.fnReloadAjax(action_url);
			//oTable.fnClearTable( 0 );
			//oTable.fnDraw();
		}*/
		
		
	/*var buttonfunction = $('#ActivateBut').attr('onClick');
	alert(typeof(buttonfunction));
	alert(buttonfunction);
	/*var splitedFunction = buttonfunction.split(",");
	alert(splitedFunction[5]);
	splitedFunction[5]="'"+active+"');";
	var newFunction = splitedFunction.join(',');
	$('#ActivateBut').attr('onClick',newFunction);

	var buttonfunction = $('#DeactivateBut').attr('onClick');
	var splitedFunction = buttonfunction.split(",");
	splitedFunction[5]="'"+active+"');";
	newFunction = splitedFunction.join(',');
	$('#DeactivateBut').attr('onClick',newFunction);*/
	
}



function executeGridActive(action_url,active)
{
	$('#ActBtns').click( function() {
            oSettings.oApi._fnPageChange( oSettings, "previous" );
            fnCallbackDraw( oSettings );
        } );
	alert(action_url+' '+active);
	oTable.fnClearTable( 0 );
	oTable.fnClearTable();
	//oTable.fnLoadTable();
	
	alert(action_url+' '+active);
	//if (oTable == 'undefined') {
		alert(oTable);
			 oTable = $('#example').dataTable( {
				"sDom": '<"top"f>rt<"bottom"lpi><"clear">',
				"sPaginationType": "full_numbers",
				"bJQueryUI": true,
				"bAutoWidth" : false,
				"aaSorting": [[0, 'DESC']],
				//"sScrollY": 280,
				//"sScrollX": "90%",
				//"sScrollXInner": "110%",
				"aLengthMenu": [[10, 25, 50], [10, 25, 50]],
				"bProcessing": true,
				"bServerSide": true,
				"bDestroy": true,
				//"bRetrieve": true,
				"sAjaxSource": action_url,
				"fnServerData": function ( sSource, aoData, fnCallback ) {
					/* Add some extra data to the aoData - FOR POSTING CUSTOM VALUES */
					aoData.push( { "name": "active", "value": active } );
					//aoData.push( { "name": "sEcho", "value": $('#sEcho').val() } );
					//$('#sEcho').val(aoData[0].value);
					$.ajax( {
						"dataType": 'json', 
						"type": "POST", 
						"url": sSource, 
						"data": aoData, 
						"success": fnCallback
					} );
				}
			} );
		
			
	
}



function addItem(redirect_url)
{
	window.location.href = redirect_url;
}

function popUp(title,redirect_url,a_url)
{
	//window.location.href = redirect_url;
	tb_show(title,a_url);
}


function editItem(checkbox_name,url)
{
	var chks = document.getElementsByName(checkbox_name+'[]');
	var c_value = new Array();
	var hasChecked = false;
	var totval=0;
	for (var i = 0; i < chks.length; i++)
	{
		if (chks[i].checked)
		{
			hasChecked = true;
			c_value = chks[i].value;
			totval++;
		}
		
	}
	if (hasChecked == false)
	{
		alert("Please select at least one item.");
		return false;
	}
	if(totval > 1)
	{
		alert("Please select only one item.");
		return false;
	}
	window.location.href = url+c_value;
}

function changeStatus(checkbox_name,redirect_url,action_url,succ_msg,msg,active,type)
{
	if(msg!='')
	{
		msg = msg;
	} else {
		msg = 'status';
	}
	var chks = document.getElementsByName(checkbox_name+'[]');
	var c_value = new Array();
	var hasChecked = false;
	var totval=0;
	for (var i = 0; i < chks.length; i++)
	{
		if (chks[i].checked)
		{
			hasChecked = true;
			c_value = c_value + chks[i].value + ",";
			totval++;
		}
		
	}
	if (hasChecked == false)
	{
		alert("Please select at least one item.");
		return false;
	}
	if(confirm('Are you sure to '+msg +' ?'))
	{

	$.ajax({
		type: "POST",
		dataType: "json",
		url: action_url,
		data: {items:c_value,type:type},
		success: function(data){
			alert(totval +' '+ succ_msg);
			//executeGrid(redirect_url,active);
			oTable.fnClearTable( 0 );
			oTable.fnDraw();
		}
	});
	}
}

function changeRoleStatus(checkbox_name,redirect_url,action_url,succ_msg,msg,active)
{
	if(msg!='')
	{
		msg = msg;
	} else {
		msg = 'status';
	}
	var chks = document.getElementsByName(checkbox_name+'[]');
	var c_value = new Array();
	var hasChecked = false;
	var totval=0;
	for (var i = 0; i < chks.length; i++)
	{
		if (chks[i].checked)
		{
			hasChecked = true;
			c_value = c_value + chks[i].value + ",";
			totval++;
		}
		
	}

	if(totval==0){
		alert("Please select an item.");
		return false;
	}
	
	if(totval>1){
		alert("Please select only one item.");
		return false;
	}
	if(confirm('Are you sure to '+msg +' ?'))
	{

		$.ajax({
			type: "POST",
			dataType: "json",
			url: action_url,
			data: "items="+c_value,
			success: function(data){
				if(data.resStatus==1){
					alert(totval +' '+ succ_msg);
					//executeGrid(redirect_url,active);
					oTable.fnClearTable( 0 );
					oTable.fnDraw();
					
				} else {
					alert(data.statusMessage);
				}
				
			}
		});
	}
}


function changeStatusPopUp(checkbox_name,redirect_url,action_url,succ_msg,msg,active,toUrl)
{
	if(msg!='')
	{
		msg = msg;
	} else {
		msg = 'status';
	}
	var chks = document.getElementsByName(checkbox_name+'[]');
	var chks = document.getElementsByName(checkbox_name+'[]');
	var c_value = new Array();
	var hasChecked = false;
	var totval=0;
	for (var i = 0; i < chks.length; i++)
	{
		if (chks[i].checked)
		{
			hasChecked = true;
			c_value = c_value + chks[i].value + ",";
			totval++;
		}
		
	}
	if (hasChecked == false)
	{
		alert("Please select at least one item.");
		return false;
	}
	if(active == 1 && totval>1)
	{
		alert('Please Select Only One Item');
		return false; 
	}
	//if(confirm('Are you sure to '+msg +' ?'))
	//{
		tb_show(msg,toUrl+'&ids='+c_value+'&toUrl='+action_url);
	//}
}

function changeStatusPopUpEntity(name,checkbox_name,redirect_url,action_url,succ_msg,msg,active,toUrl)
{
	//alert(name);
	if(msg!='')
	{
		msg = msg;
	} else {
		msg = 'status';
	}
	var chks = document.getElementsByName(checkbox_name+'[]');
	var eName = document.getElementsByName(name+'[]');
	var c_value = new Array();
	var hasChecked = false;
	var totval=0;
	for (var i = 0; i < chks.length; i++)
	{
		if (chks[i].checked)
		{
			hasChecked = true;
			c_value = c_value + chks[i].value + ",";
			eNames = eName[i].value;
			totval++;
		}
		
	}
	//alert(eNames);
	if (hasChecked == false)
	{
		alert("Please select at least one item.");
		return false;
	}
	if(active == 1 && totval>1)
	{
		alert('Please Select Only One Item');
		return false; 
	}
	//if(confirm('Are you sure to '+msg +' ?'))
	//{
		tb_show(msg+' - '+eNames,toUrl+'&ids='+c_value+'&toUrl='+action_url);
	//}
}



function checkAll(val,checkbox_name)
{
	var chks = document.getElementsByName(checkbox_name+'[]');
	var check_flag = "";
	
	if(val=='1')
		check_flag=true;
	else
		check_flag=false;
	for (var i = 0; i < chks.length; i++)
	{
		chks[i].checked = check_flag;
	}
}

function applyFilter(url,val){
if(val=='All')
{
	document.getElementById('villages').style.display='none';
	executeGrid(url,val);
}
else if(val=='villages')
{
	document.getElementById('villages').style.display='block';
}
else if(val=='username')
{
	document.getElementById('villages').style.display='none';
	showSearchSection();
}
else
{
	executeGrid(url,val);
}
}

function showFilterSection(){
	if(document.getElementById('filterSecion').style.display=='block'){
		document.getElementById('filterSecion').style.display='none';
	}
	else{
		document.getElementById('filterSecion').style.display='block';
	}
}

function showSearchSection(){
	if(document.getElementById('example_filter').style.display=='none'){
		document.getElementById('example_filter').style.display='block';
	}
	else{
		document.getElementById('example_filter').style.display='none';
	}
}

function executeGridResident(action_url,active)
{
var active=$('#status_change').val();
var village_search=$('#village_search').val();
if($('#village_search').val().length<=1)
{
village_search='';
}
var client_search=$('#client_search').val();
if($('#client_search').val().length<=1)
{
client_search='';
}
    //if (typeof oTable == 'undefined') {
             oTable = $('#example').dataTable( {
                "sDom": '<"top"f>rt<"bottom"lpi><"clear">',
                //"sDom": 'rt<"bottom"lpi><"clear">',
                "sPaginationType": "full_numbers",
                "bJQueryUI": true,
                "bAutoWidth" : false,
                "aaSorting": [[0, 'DESC']],
                "aoColumns": [{ "bSortable": false },null,null,{ "bSortable": false },{ "bSortable": false },null,null,null,null,null,{ "bSortable": false }],
                //"sScrollY": 280,
                //"sScrollX": "90%",
                //"sScrollXInner": "110%",
                "iDisplayLength": 25,
                "aLengthMenu": [[25, 50, 100], [25, 50, 100]],
                "bProcessing": true,
                "bServerSide": true,
                "bDestroy": true,
                //"bRetrieve": true,
                "sAjaxSource": action_url,
                "fnServerData": function ( sSource, aoData, fnCallback ) {
                    /* Add some extra data to the aoData - FOR POSTING CUSTOM VALUES */
                    aoData.push( { "name": "active", "value": active } );
                    aoData.push({ "name": "village_search", "value": village_search });
                    aoData.push({ "name": "client_search", "value": client_search });
                    //aoData.push( { "name": "sEcho", "value": $('#sEcho').val() } );
                    //$('#sEcho').val(aoData[0].value);
                    $.ajax( {
                        "dataType": 'json', 
                        "type": "POST", 
                        "url": sSource, 
                        "data": aoData, 
                        "success": fnCallback
                    } );
                }
            } );
            
            document.getElementById('example_filter').style.display='none';
            
            
        /*}
        else
        {


            oTable.fnDraw();
            //alert(typeof oTable);
            //oTable.fnReloadAjax();
            //oTable.fnReloadAjax(action_url);
            //oTable.fnClearTable( 0 );
            //oTable.fnDraw();
        }*/
        
        
    /*var buttonfunction = $('#ActivateBut').attr('onClick');
    alert(typeof(buttonfunction));
    alert(buttonfunction);
    /*var splitedFunction = buttonfunction.split(",");
    alert(splitedFunction[5]);
    splitedFunction[5]="'"+active+"');";
    var newFunction = splitedFunction.join(',');
    $('#ActivateBut').attr('onClick',newFunction);

    var buttonfunction = $('#DeactivateBut').attr('onClick');
    var splitedFunction = buttonfunction.split(",");
    splitedFunction[5]="'"+active+"');";
    newFunction = splitedFunction.join(',');
    $('#DeactivateBut').attr('onClick',newFunction);*/
    
}